package cn.mw.cmdb.entity;

import cn.mw.cmdb.util.DataCreateInfo;
import cn.mw.components.mongodb.anno.AutoId;
import cn.mw.components.mongodb.anno.CMDBProperty;
import cn.mw.microMonitorCommon.entity.cmdb.DataSourceFilterTypeInfo;
import cn.mw.microMonitorCommon.entity.cmdb.DataSourcePropertyRelInfo;
import cn.mw.microMonitorCommon.enums.DatasourceSyncMethodEnum;
import cn.mw.microMonitorCommon.enums.EngineTypeEnum;
import cn.mw.microMonitorCommon.enums.FilterRuleEnum;
import com.alibaba.nacos.common.utils.CollectionUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Document("mw_cmdbmd_datasource")
public class DataSourceInfo implements DataCreateInfo, Serializable {

    @CMDBProperty(searchType = PropertyTypeString.ID, searchPrefix = "_")
    @AutoId
    @Schema(description = "id")
    private String id;

    @Schema(description = "类型")
    @CMDBProperty
    private String datasourceType;

    @Schema(description = "类型名称")
    private String datasourceTypeName;

    @Schema(description = "引擎类型")
    @CMDBProperty
    private EngineTypeEnum engineType;

    @Schema(description = "分组ID")
    @CMDBProperty(searchType = PropertyTypeString.ID, keywordSearch = true)
    private String groupId;

    @Schema(description = "分组路径")
    @CMDBProperty(searchType = PropertyTypeString.ID, keywordSearch = true)
    private List<String> groupPath;

    @Schema(description = "引擎ID")
    @CMDBProperty
    private Integer engineId;

    @Schema(description = "引擎名称")
    @CMDBProperty(searchType = PropertyTypeString.ID)
    private String engineName;

    @Schema(description = "名称")
    @CMDBProperty(searchType = PropertyTypeString.ID)
    private String ruleName;

    @Schema(description = "过滤规则")
    @CMDBProperty
    private FilterRuleEnum filterRule;

    @Schema(description = "规则列表")
    @CMDBProperty
    private List<DataSourceFilterTypeInfo> dataSourceFilterTypeList;

    @Schema(description = "目标模型路径")
    @CMDBProperty
    private List<String> targetModelIdPath;

    @Schema(description = "映射规则")
    @CMDBProperty
    private List<DataSourcePropertyRelInfo> mappingRule = new ArrayList<>();

    @Schema(description = "同步方式")
    @CMDBProperty
    private DatasourceSyncMethodEnum syncMethod;

    @Schema(description = "同步类型")
    @CMDBProperty
    private String syncType;

    @Schema(description = "事件间隔")
    @CMDBProperty
    private Integer syncTime;

    @Schema(description = "间隔单位")
    @CMDBProperty
    private String syncTimeUnit;

    @Schema(description = "同步任务Id")
    @CMDBProperty
    private Long jobId;

    @Schema(description = "是否启动扫描")
    @CMDBProperty
    private Boolean autoScanFlag;

    @Schema(description = "调用服务器实例id")
    @CMDBProperty
    private List<String> agentInstanceIds;

    //所属用户组
    @CMDBProperty
    private List<Long> groups;

    //所属用户
    @CMDBProperty
    private List<Long> users;

    //所属机构id
    //便于数据权限查询时,只查询自己机构的数据
    @CMDBProperty
    private List<Long> orgIds;

    @CMDBProperty
    private List<List<Long>> orgs;

    public void setOrgs(List<List<Long>> orgs) {
        this.orgIds = new ArrayList<>();
        if (null != orgs) {
            for (List value : orgs) {
                Long valueLong = Long.valueOf(value.get(value.size() - 1).toString());
                orgIds.add(valueLong);
            }
        }
        this.orgs = orgs;
    }

    public void setGroupPath(List<String> groupPath) {
        this.groupPath = groupPath;
        if (CollectionUtils.isNotEmpty(groupPath)) {
            this.groupId = groupPath.get(groupPath.size() - 1);
        }
    }
}
